<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是内核模块的编译与加载相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV10-01-内核模块-01-编译与加载">
<meta property="og:url" content="https://sumumm.github.io/post/b24f362c.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是内核模块的编译与加载相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831090450726.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831091956871.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831100115565.png">
<meta property="article:published_time" content="2023-07-08T10:58:04.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.002Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV10-驱动开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831090450726.png">


<link rel="canonical" href="https://sumumm.github.io/post/b24f362c.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/b24f362c.html","path":"post/b24f362c.html","title":"LV10-01-内核模块-01-编译与加载"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV10-01-内核模块-01-编译与加载 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97%E7%AE%80%E4%BB%8B"><span class="nav-text">一、内核模块简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">1. 内核模块的概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97%E7%9A%84%E7%89%B9%E7%82%B9"><span class="nav-text">2. 内核模块的特点</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%A8%A1%E5%9D%97%E7%9A%84%E5%8A%A0%E8%BD%BD"><span class="nav-text">二、模块的加载</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%9D%99%E6%80%81%E5%8A%A0%E8%BD%BD"><span class="nav-text">1. 静态加载</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E7%BC%96%E5%86%99%E6%A8%A1%E5%9D%97%E6%BA%90%E6%96%87%E4%BB%B6"><span class="nav-text">1.1 编写模块源文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BF%AE%E6%94%B9Kconfig"><span class="nav-text">1.2 修改Kconfig</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E4%BF%AE%E6%94%B9Makefile"><span class="nav-text">1.3 修改Makefile</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E9%85%8D%E7%BD%AE%E6%96%B0%E5%8A%9F%E8%83%BD%E7%BC%96%E8%AF%91%E6%96%B9%E5%BC%8F"><span class="nav-text">1.4 配置新功能编译方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-%E7%BC%96%E8%AF%91%E5%86%85%E6%A0%B8%E5%B9%B6%E6%B5%8B%E8%AF%95"><span class="nav-text">1.5 编译内核并测试</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%8A%A8%E6%80%81%E5%8A%A0%E8%BD%BD"><span class="nav-text">2. 动态加载</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E6%A8%A1%E5%9D%97%E7%BC%96%E8%AF%91"><span class="nav-text">2.1 模块编译</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1-%E6%A8%A1%E5%9D%97%E6%BA%90%E7%A0%81%E5%9C%A8linux%E5%86%85%E6%A0%B8%E6%BA%90%E7%A0%81%E4%B8%AD"><span class="nav-text">2.1.1 模块源码在linux内核源码中</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2-%E6%A8%A1%E5%9D%97%E6%BA%90%E7%A0%81%E7%8B%AC%E7%AB%8B%E5%9C%A8%E5%85%B6%E4%BB%96%E7%9B%AE%E5%BD%95"><span class="nav-text">2.1.2 模块源码独立在其他目录</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%91%BD%E4%BB%A4%E4%BB%8B%E7%BB%8D"><span class="nav-text">2.2 命令介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-file"><span class="nav-text">2.2.1 file</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-insmod"><span class="nav-text">2.2.2 insmod</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-3-lssmod"><span class="nav-text">2.2.3 lssmod</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-4-rmsmod"><span class="nav-text">2.2.4 rmsmod</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-5-dmesg"><span class="nav-text">2.2.5 dmesg</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%8A%A0%E8%BD%BD%E6%B5%8B%E8%AF%95"><span class="nav-text">2.3 加载测试</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E6%A8%A1%E5%9D%97%E5%9F%BA%E7%A1%80%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90"><span class="nav-text">三、模块基础代码解析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%87%A0%E4%B8%AA%E5%A4%B4%E6%96%87%E4%BB%B6"><span class="nav-text">1. 几个头文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%A8%A1%E5%9D%97%E5%8A%A0%E8%BD%BD%E4%B8%8E%E5%8D%B8%E8%BD%BD"><span class="nav-text">2. 模块加载与卸载</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E6%A8%A1%E5%9D%97%E5%8A%A0%E8%BD%BD"><span class="nav-text">2.1 模块加载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E6%A8%A1%E5%9D%97%E5%8D%B8%E8%BD%BD"><span class="nav-text">2.2 模块卸载</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%A8%A1%E5%9D%97%E4%BF%A1%E6%81%AF%E5%AE%8F"><span class="nav-text">3. 模块信息宏</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-MODULE-LICENSE"><span class="nav-text">3.1 MODULE_LICENSE</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-MODULE-AUTHOR"><span class="nav-text">3.2 MODULE_AUTHOR</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-MODULE-DESCRIPTION"><span class="nav-text">3.3 MODULE_DESCRIPTION</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-MODULE-ALIAS"><span class="nav-text">3.4 MODULE_ALIAS</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%A8%A1%E5%9D%97%E5%9F%BA%E7%A1%80%E6%A1%86%E6%9E%B6"><span class="nav-text">4. 模块基础框架</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81Makefile"><span class="nav-text">四、Makefile</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%A3%E7%A0%81%E8%A7%A3%E6%9E%90"><span class="nav-text">1. 代码解析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%87%A0%E4%B8%AA%E5%8F%98%E9%87%8F"><span class="nav-text">1.1 几个变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E9%80%89%E6%8B%A9%E5%86%85%E6%A0%B8%E6%BA%90%E7%A0%81%E7%9B%AE%E5%BD%95"><span class="nav-text">1.2 选择内核源码目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E7%BC%96%E8%AF%91%E5%91%BD%E4%BB%A4"><span class="nav-text">1.3 编译命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-obj-m"><span class="nav-text">1.4 obj-m </span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%A4%9A%E6%BA%90%E6%96%87%E4%BB%B6%E7%BC%96%E8%AF%91"><span class="nav-text">2. 多源文件编译</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/b24f362c.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV10-01-内核模块-01-编译与加载 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV10-01-内核模块-01-编译与加载
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-08 18:58:04" itemprop="dateCreated datePublished" datetime="2023-07-08T18:58:04+08:00">2023-07-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV10-驱动开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.9k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>22 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是内核模块的编译与加载相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center" width=150>Linux开发板</td>        <td align="left">华清远见 底板: FS4412_DEV_V5 核心板: FS4412 V2</td>      </tr>    <tr>        <td align="center" width=150>u-boot</td>        <td align="left">2013.01</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="center">文件</td><td align="center">下载链接</td></tr>    <tr><td align="left">---</td><td align="left"><a href="https://wwz.lanzouy.com/iH1vO09jxavi" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>



<h1 id="一、内核模块简介"><a href="#一、内核模块简介" class="headerlink" title="一、内核模块简介"></a><font size=3>一、内核模块简介</font></h1><h2 id="1-内核模块的概念"><a href="#1-内核模块的概念" class="headerlink" title="1. 内核模块的概念"></a><font size=3>1. 内核模块的概念</font></h2><p>什么是<code>Linux</code>内核模块？我查了百度：<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/Linux%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97/1968772">Linux内核模块_百度百科 (baidu.com)  <i class="fa fa-external-link-alt"></i></a></p>
<p>尽管<code>Linux</code>是”单块内核“(<code>monolithic</code>)的操作系统——这是说整个系统内核都运行于一个单独的保护域中，但是<code>linux</code>内核是模块化组成的，它允许内核在运行时动态地向其中插入或从中删除代码。这些代码(包括相关的子线程、数据、函数入口和函数出口)被一并组合在一个单独的二进制镜像中，即所谓的可装载内核模块中，或简称为模块。</p>
<p>支持模块的好处是基本内核镜像尽可能的小，因为可选的功能和驱动程序可以利用模块形式再提供。模块允许我们方便地删除和重新载入内核代码，也方便了调试工作。而且当热插拔新设备时，可通过命令载入新的驱动程序。</p>
<p>类似于浏览器、<code>eclipse</code>这些软件的插件开发，<code>Linux</code>提供了一种可以向正在运行的内核中插入新的代码段、在代码段不需要继续运行时也可以从内核中移除的机制，这个可以被插入、移除的代码段被称为内核模块。可用于加载到<code>linux</code>内核的文件后缀为<code>.ko</code>，<code>.ko</code>文件是<code>kernel object</code>文件，也就是<code>kernel</code>下的模块加载文件。</p>
<p>内核模块的<strong>本质</strong>：一段隶属于内核的“<strong>动态</strong>”代码，与其它内核代码是同一个运行实体，共用同一套运行资源，只是存在形式上是独立的。</p>
<h2 id="2-内核模块的特点"><a href="#2-内核模块的特点" class="headerlink" title="2. 内核模块的特点"></a><font size=3>2. 内核模块的特点</font></h2><p>内核模块的出现，解决了以下问题：</p>
<ul>
<li>单内核扩展性差的缺点。</li>
<li>减小内核镜像文件体积，一定程度上节省内存资源。</li>
<li>提高开发效率。</li>
</ul>
<p>但是内核模块也有相应的缺点，就是它不能彻底解决稳定性低的缺点：内核模块代码出错可能会导致整个系统崩溃。</p>
<h1 id="二、模块的加载"><a href="#二、模块的加载" class="headerlink" title="二、模块的加载"></a><font size=3>二、模块的加载</font></h1><p>模块的加载有两种方式：一是静态加载法，在<code>linux</code>源码中进行修改，配置，然后直接编译进<code>linux</code>内核；另一种是动态加载法，编写好模块后，通过一些列命令讲模块插入到<code>linux</code>内核。</p>
<h2 id="1-静态加载"><a href="#1-静态加载" class="headerlink" title="1. 静态加载"></a><font size=3>1. 静态加载</font></h2><p>静态加载就是新功能源码与内核其它代码一起编译进<code>uImage</code>文件内，这种编译方式不会生成<code>.ko</code>内核模块文件，只会生成一个<code>.o</code>文件。</p>
<h3 id="1-1-编写模块源文件"><a href="#1-1-编写模块源文件" class="headerlink" title="1.1 编写模块源文件"></a><font size=3>1.1 编写模块源文件</font></h3><p>我们需要编写一个模块源文件用于测试，我们在<code>linux-3.14/driver/char/</code>目录下新建<code>myhello.c</code>文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14/driver/char/   # 进入相应的目录</span><br><span class="line">vim myhello.c     # 创建文件</span><br></pre></td></tr></table></figure>

<p>并添加以下内容：</p>
<details class="folding-tag" blue><summary> 点击查看 myhello.c 测试源码 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** =====================================================</span></span><br><span class="line"><span class="comment"> * Copyright © hk. 2022-2025. All rights reserved.</span></span><br><span class="line"><span class="comment"> * File name: myhello.c</span></span><br><span class="line"><span class="comment"> * Author   : qidaink</span></span><br><span class="line"><span class="comment"> * Date     : 2022-08-23</span></span><br><span class="line"><span class="comment"> * Version  : </span></span><br><span class="line"><span class="comment"> * Description: </span></span><br><span class="line"><span class="comment"> * Others   : </span></span><br><span class="line"><span class="comment"> * Log      : </span></span><br><span class="line"><span class="comment"> * ======================================================</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span>   <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span>     <span class="comment">/* module_init module_exit */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块入口函数 */</span></span><br><span class="line"><span class="type">int</span> __init <span class="title function_">myhello_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	printk(<span class="string">&quot;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n&quot;</span>);</span><br><span class="line">    printk(<span class="string">&quot;myhello is running!\n&quot;</span>);</span><br><span class="line">	printk(<span class="string">&quot;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块出口函数 */</span></span><br><span class="line"><span class="type">void</span> __exit <span class="title function_">myhello_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	printk(<span class="string">&quot;myhello will exit\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 将上面两个函数指定为驱动的入口和出口函数 */</span></span><br><span class="line">module_init(myhello_init);</span><br><span class="line">module_exit(myhello_exit);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块信息(通过 modinfo myhello 查看) */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL&quot;</span>);               <span class="comment">/* 源码的许可证协议 */</span></span><br><span class="line">MODULE_AUTHOR(<span class="string">&quot;qidaink&quot;</span>);            <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br><span class="line">MODULE_DESCRIPTION(<span class="string">&quot;Description&quot;</span>);   <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br><span class="line">MODULE_ALIAS(<span class="string">&quot;module&#x27;s other name&quot;</span>); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="1-2-修改Kconfig"><a href="#1-2-修改Kconfig" class="headerlink" title="1.2 修改Kconfig"></a><font size=3>1.2 修改<code>Kconfig</code></font></h3><p>此步骤可以向<code>linux</code>的图形配置界面添加新功能配置选项。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd  ~/5linux/linux-3.14/driver/char/  # 进入myhello.c的同级目录</span><br><span class="line">vim Kconfig                           # 打开相关配置文件</span><br></pre></td></tr></table></figure>

<p>找到如下语句（应该在第<code>7</code>行）：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">source &quot;drivers/tty/Kconfig&quot;</span><br></pre></td></tr></table></figure>

<p>在该语句下边添加以下内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">config MY_HELLO</span><br><span class="line">	tristate &quot;This is my hello test!&quot;</span><br><span class="line">	help</span><br><span class="line">		This is a test for kernel new function</span><br></pre></td></tr></table></figure>

<p>然后保存退出即可，我们可以试一下有什么效果，我们回到顶层源码目录下，打开图形配置界面：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd  ~/5linux/linux-3.14/</span><br><span class="line">make menuconfig</span><br></pre></td></tr></table></figure>

<p>我们按以下层级找到我们新添加的配置项：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Device Drivers  ---&gt;</span><br><span class="line">        Character devices  ---&gt;</span><br><span class="line">            &lt; &gt; This is a hello test (NEW)</span><br></pre></td></tr></table></figure>

<p>如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831090450726.png" alt="image-20220831090450726" style="zoom:33%;" />

<h3 id="1-3-修改Makefile"><a href="#1-3-修改Makefile" class="headerlink" title="1.3 修改Makefile"></a><font size=3>1.3 修改<code>Makefile</code></font></h3><p>我们添加了新源码文件，自然要参与编译的，我们打开<code>myhello.c</code>同级目录下的<code>Makefile</code>：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ~/5linux/linux-3.14/drivers/char/Makefile</span><br></pre></td></tr></table></figure>

<p>我们找到如下语句（应该在第<code>18</code>行）：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj-<span class="variable">$(CONFIG_BFIN_OTP)</span>          += bfin-otp.o</span><br></pre></td></tr></table></figure>

<p>我们在这一句下边添加如下内容：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj-<span class="variable">$(CONFIG_MY_HELLO)</span>          += myhello.o</span><br></pre></td></tr></table></figure>

<p>【注意】这里需要是<code>CONFIG_xxx</code>，这个<code>xxx</code>就是上边修改<code>Kconfig</code>的时候添加的<code>config MY_HELLO</code>的<code>MY_HELLO</code>。</p>
<h3 id="1-4-配置新功能编译方式"><a href="#1-4-配置新功能编译方式" class="headerlink" title="1.4 配置新功能编译方式"></a><font size=3>1.4 配置新功能编译方式</font></h3><p>我们这里打开<code>linux</code>内核图形配置界面:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14</span><br><span class="line">make menuconfig</span><br></pre></td></tr></table></figure>

<p>然后将新功能修改为<code>*</code>，表示将新功能编译到内核中。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Device Drivers  ---&gt;</span><br><span class="line">        Character devices  ---&gt;</span><br><span class="line">            &lt;*&gt; This is a hello test (NEW)</span><br></pre></td></tr></table></figure>

<h3 id="1-5-编译内核并测试"><a href="#1-5-编译内核并测试" class="headerlink" title="1.5 编译内核并测试"></a><font size=3>1.5 编译内核并测试</font></h3><ul>
<li>编译内核</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14 # 回到顶层源码目录下</span><br><span class="line">make uImage -j4        # 编译内核</span><br></pre></td></tr></table></figure>

<p>出现以下提示信息表示编译成功：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Image Name:   Linux-3.14.0-gc6094bb-dirty</span><br><span class="line">Created:      Wed Aug 31 09:17:19 2022</span><br><span class="line">Image Type:   ARM Linux Kernel Image (uncompressed)</span><br><span class="line">Data Size:    2931456 Bytes = 2862.75 kB = 2.80 MB</span><br><span class="line">Load Address: 40008000</span><br><span class="line">Entry Point:  40008000</span><br><span class="line">  Image arch/arm/boot/uImage is ready</span><br></pre></td></tr></table></figure>

<ul>
<li>拷贝内核文件并重启开发板测试</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cp arch/arm/boot/uImage ~/3tftp/fs4412/</span><br></pre></td></tr></table></figure>

<p>开发板重启后，启动内核时有以下信息出现，则说明新功能添加成功：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831091956871.png" alt="image-20220831091956871" style="zoom:50%;" />

<h2 id="2-动态加载"><a href="#2-动态加载" class="headerlink" title="2. 动态加载"></a><font size=3>2. 动态加载</font></h2><p>新功能源码与内核其它源码不一起编译，而是独立编译成内核的插件(被称为内核模块）文件<code>.ko</code>。然后通过一些命令进行插入或者卸载。</p>
<h3 id="2-1-模块编译"><a href="#2-1-模块编译" class="headerlink" title="2.1 模块编译"></a><font size=3>2.1 模块编译</font></h3><h4 id="2-1-1-模块源码在linux内核源码中"><a href="#2-1-1-模块源码在linux内核源码中" class="headerlink" title="2.1.1 模块源码在linux内核源码中"></a><font size=3>2.1.1 模块源码在<code>linux</code>内核源码中</font></h4><p>我们还是使用上边静态加载使创建的文件<code>myhello.c</code>，该文件包含在<code>linux</code>内核源码中，编译模块的步骤如下：</p>
<ul>
<li>（1）编写模块源码文件（同<code>1.1</code>）</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14/driver/char/   # 进入相应的目录</span><br><span class="line">vim myhello.c     # 创建文件</span><br></pre></td></tr></table></figure>

<ul>
<li>（2）配置<code>Kconfig</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd  ~/5linux/linux-3.14/driver/char/  # 进入myhello.c的同级目录</span><br><span class="line">vim Kconfig                           # 打开相关配置文件</span><br></pre></td></tr></table></figure>

<ul>
<li>（3）修改<code>Makefile</code>（同<code>1.3</code>）</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ~/5linux/linux-3.14/drivers/char/Makefile</span><br></pre></td></tr></table></figure>

<ul>
<li>（4）修改新功能编译方式</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14 # 回到顶层源码目录下</span><br><span class="line">make menuconfig</span><br></pre></td></tr></table></figure>

<p>然后将新功能前边配置改为<code>M</code>，表示编译成独立的模块：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Device Drivers  ---&gt;</span><br><span class="line">        Character devices  ---&gt;</span><br><span class="line">            &lt;M&gt; This is a hello test (NEW)</span><br></pre></td></tr></table></figure>

<ul>
<li>（5）重新编译内核</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14 # 回到顶层源码目录下</span><br><span class="line">make uImage -j4        # 编译内核</span><br></pre></td></tr></table></figure>

<ul>
<li>（6）模块编译</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd ~/5linux/linux-3.14  # 回到顶层源码目录下</span><br><span class="line">make modules -j4        # 编译模块</span><br></pre></td></tr></table></figure>

<p>完成模块编译后，我们就会发现在<code>myhello.c</code>目录下生成了一个<code>myhello.ko</code>文件，这就是我们后边要使用的内核模块文件。</p>
<h4 id="2-1-2-模块源码独立在其他目录"><a href="#2-1-2-模块源码独立在其他目录" class="headerlink" title="2.1.2 模块源码独立在其他目录"></a><font size=3>2.1.2 模块源码独立在其他目录</font></h4><p>这样每次都要使用<code>linux</code>源码目录下操作，文件也太多了，其实我们也可以在其他的目录单独创建模块驱动源码文件，然后调用<code>linux</code>源码进行编译，生成我们所需要的<code>.ko</code>内核模块文件。一般步骤如下：</p>
<ul>
<li>（1）新建一个目录用于存放我们的模块源码</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir ~/myhello # 新建一个目录</span><br><span class="line">cd ~/myhello    # 进入相应目录</span><br></pre></td></tr></table></figure>

<ul>
<li>（2）编写模块源码文件</li>
</ul>
<p>这里我直接拷贝之前的文件到这里来：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cp ~/5linux/linux-3.14/drivers/char/myhello.c .</span><br></pre></td></tr></table></figure>

<ul>
<li>（3）编写<code>Makefile</code>文件</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看 Makefile 文件内容 </summary>
              <div class='content'>
              <figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">##============================================================================#</span></span><br><span class="line"><span class="comment"># Copyright © hk. 2022-2025. All rights reserved.</span></span><br><span class="line"><span class="comment"># File name  : Makefile</span></span><br><span class="line"><span class="comment"># Author     : qidaink</span></span><br><span class="line"><span class="comment"># Date       : 2022-08-28</span></span><br><span class="line"><span class="comment"># Version    : </span></span><br><span class="line"><span class="comment"># Description: 驱动模块编译Makefile</span></span><br><span class="line"><span class="comment"># Others     : </span></span><br><span class="line"><span class="comment"># Log        : </span></span><br><span class="line"><span class="comment">##============================================================================#</span></span><br><span class="line"><span class="comment">##</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 模块名和模块测试APP名称</span></span><br><span class="line">MODULE_NAME      := myhello</span><br><span class="line"><span class="comment"># NFS 共享目录</span></span><br><span class="line">ROOTFS           ?= /home/hk/4nfs/rootfs</span><br><span class="line">MODULE_DIR       ?= <span class="variable">$(ROOTFS)</span>/01myDrivers</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(KERNELRELEASE)</span>,)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选择可执行文件运行的平台</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(ARCH)</span>, arm)</span><br><span class="line">KERNELDIR        ?= /home/hk/5linux/linux-3.14</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">KERNELDIR        ?= /lib/modules/<span class="variable">$(<span class="built_in">shell</span> uname -r)</span>/build</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line">PWD              := <span class="variable">$(<span class="built_in">shell</span> pwd)</span></span><br><span class="line"><span class="comment"># 编译模块和测试程序</span></span><br><span class="line"><span class="section">modules:</span></span><br><span class="line">	<span class="variable">$(MAKE)</span> -C <span class="variable">$(KERNELDIR)</span> M=<span class="variable">$(PWD)</span> modules</span><br><span class="line"></span><br><span class="line"><span class="section">modules_install:</span></span><br><span class="line">	<span class="variable">$(MAKE)</span> -C <span class="variable">$(KERNELDIR)</span> M=<span class="variable">$(PWD)</span> modules INSTALL_MOD_PATH=<span class="variable">$(ROOTFS)</span> modules_install</span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝相关文件到nfs共享目录</span></span><br><span class="line"><span class="section">copy:</span></span><br><span class="line">	sudo cp <span class="variable">$(MODULE_NAME)</span>.ko <span class="variable">$(MODULE_DIR)</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf  *.o  *.ko  .*.cmd  *.mod.*  modules.order  Module.symvers   .tmp_versions .cache.mk</span><br><span class="line"></span><br><span class="line"><span class="section">help:</span></span><br><span class="line">	@echo <span class="string">&quot;\033[1;32m================================ Help ================================\033[0m&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;Ubuntu may need to add sudo:&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;insmod &lt;module_name&gt;.ko   # Load module&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;rmmod &lt;module_name&gt;       # Uninstall the module&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;dmesg -C                  # Clear the kernel print information&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;lsmod                     # Check the kernel modules that have been inserted&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;dmesg                     # View information printed by the kernel&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;file &lt;module_name&gt;.ko     # View \&quot;.Ko\&quot; file information&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;make ARCH=arm             # arm platform&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;\033[1;32m======================================================================\033[0m&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="section">print:</span></span><br><span class="line">	@echo <span class="string">&quot;KERNELDIR = <span class="variable">$(KERNELDIR)</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">CONFIG_MODULE_SIG=n</span><br><span class="line">obj-m            += <span class="variable">$(MODULE_NAME)</span>.o</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<ul>
<li>（4）编译模块</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">make          # 编译可在Ubuntu中运行的模块</span><br><span class="line">make ARCH=arm # 编译可在ARM上运行的模块</span><br></pre></td></tr></table></figure>

<h3 id="2-2-命令介绍"><a href="#2-2-命令介绍" class="headerlink" title="2.2 命令介绍"></a><font size=3>2.2 命令介绍</font></h3><p>上边我们已经得到了<code>.ko</code>内核模块文件，下边是一些相关的命令。</p>
<h4 id="2-2-1-file"><a href="#2-2-1-file" class="headerlink" title="2.2.1 file"></a><font size=3>2.2.1 <code>file</code></font></h4><p><code>file</code>命令可用于查看指定的<code>.ko</code>文件可以用于哪种平台，一般使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">file module_name.ko</span><br></pre></td></tr></table></figure>

<p>输出信息中，带<code>x86</code>字样的适用于主机<code>ubuntu linux</code>，带<code>arm</code>字样的适用于开发板<code>linux</code>。</p>
<h4 id="2-2-2-insmod"><a href="#2-2-2-insmod" class="headerlink" title="2.2.2 insmod"></a><font size=3>2.2.2 <code>insmod</code></font></h4><p><code>insmod</code>命令可用于加载<code>.ko</code>文件，一般使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod module_name.ko</span><br></pre></td></tr></table></figure>

<p>【注意】在主机<code>Ubuntu</code>中加载模块的时候需要加上<code>sudo</code>。</p>
<h4 id="2-2-3-lssmod"><a href="#2-2-3-lssmod" class="headerlink" title="2.2.3 lssmod"></a><font size=3>2.2.3 <code>lssmod</code></font></h4><p><code>lsmod</code>命令可用显示已经加载的模块，一般使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lsmod module_name.ko</span><br></pre></td></tr></table></figure>

<p>【注意】在主机<code>Ubuntu</code>中加载模块的时候不加<code>sudo</code>，也可以正常使用。</p>
<h4 id="2-2-4-rmsmod"><a href="#2-2-4-rmsmod" class="headerlink" title="2.2.4 rmsmod"></a><font size=3>2.2.4 <code>rmsmod</code></font></h4><p><code>rmmod</code>命令可用于卸载已经加载的模块，一般使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rmmod module_name</span><br></pre></td></tr></table></figure>

<p>【注意】在主机<code>Ubuntu</code>中加载模块的时候需要加上<code>sudo</code>。</p>
<h4 id="2-2-5-dmesg"><a href="#2-2-5-dmesg" class="headerlink" title="2.2.5 dmesg"></a><font size=3>2.2.5 <code>dmesg</code></font></h4><p><code>dmesg</code>命令可用于查看内核的打印的提示信息，主要是在<code>Ubuntu</code>中加载模块的使用后使用，我们的开发板会实时在串口终端界面打提示信息，一般使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo dmesg -C  # 清除内核已打印的信息</span><br><span class="line">dmseg          # 查看内核的打印信息</span><br></pre></td></tr></table></figure>

<p>【注意】在主机<code>Ubuntu</code>中加载模块的时候需要加上<code>sudo</code>，在开发板中不需要使用该命令。</p>
<h3 id="2-3-加载测试"><a href="#2-3-加载测试" class="headerlink" title="2.3 加载测试"></a><font size=3>2.3 加载测试</font></h3><ul>
<li>拷贝<code>.ko</code>文件到根文件系统中</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo cp myhello.ko ~/4nfs/rootfs/01myDrivers/</span><br></pre></td></tr></table></figure>

<p>【注意】需要加上<code>sudo</code>，否则可能会提示没有权限拷贝。</p>
<ul>
<li>加载测试</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod myhello.ko</span><br></pre></td></tr></table></figure>

<p>若出现以下提示，则说明加载成功：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-01-%E5%86%85%E6%A0%B8%E6%A8%A1%E5%9D%97-01-%E7%BC%96%E8%AF%91%E4%B8%8E%E5%8A%A0%E8%BD%BD/img/image-20220831100115565.png" alt="image-20220831100115565" style="zoom:50%;" />

<h1 id="三、模块基础代码解析"><a href="#三、模块基础代码解析" class="headerlink" title="三、模块基础代码解析"></a><font size=3>三、模块基础代码解析</font></h1><p>【注意】一个模块必须要有的三个部分：入口函数、出口函数和<code>MODULE__LICENSE</code>。</p>
<h2 id="1-几个头文件"><a href="#1-几个头文件" class="headerlink" title="1. 几个头文件"></a><font size=3>1. 几个头文件</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span>   <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span>   <span class="comment">/* printk */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span>     <span class="comment">/* module_init module_exit */</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li><code>linux/module.h</code>：包含模块编程相关的宏定义，如<code>MODULE_LICENSE</code>。</li>
<li><code>linux/kernel.h</code>：包含内核编程最常用的函数声明，如<code>printk</code>。</li>
<li><code>linux/init.h</code>：包含模块加载与卸载函数，如<code>module_init</code>和<code>module_exit</code>。</li>
</ul>
<h2 id="2-模块加载与卸载"><a href="#2-模块加载与卸载" class="headerlink" title="2. 模块加载与卸载"></a><font size=3>2. 模块加载与卸载</font></h2><p>模块有加载和卸载两种操作，我们在编写驱动的时候需要注册这两种操作函数，模块的加载和卸载注册函数如下 ：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">module_init(xxx_init); <span class="comment">//注册模块加载函数</span></span><br><span class="line">module_exit(xxx_exit); <span class="comment">//注册模块卸载函数</span></span><br></pre></td></tr></table></figure>

<h3 id="2-1-模块加载"><a href="#2-1-模块加载" class="headerlink" title="2.1 模块加载"></a><font size=3>2.1 模块加载</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 模块入口函数 */</span></span><br><span class="line"><span class="type">int</span> __init <span class="title function_">xxx_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 入口函数具体内容 */</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module_init(xxx_init); <span class="comment">/* 注册模块加载函数 */</span></span><br></pre></td></tr></table></figure>

<p>模块加载函数位于<code>linux/init.h</code>中，<code>module_init</code> 函数用来向 <code>Linux</code> 内核注册一个模块加载函数，参数 <code>xxx_init</code> 就是需要注册的具体函数，当使用<code>insmod</code>命令加载驱动的时候，<code>xxx_init</code>这个函数就会被调用，也就是说在模块被插入到内核的时候，<code>xxx_init</code>函数被调用，它主要作用是为新功能做好准备工作。</p>
<ul>
<li><code>__init</code>说明</li>
</ul>
<blockquote>
<p> <code>__init</code>的作用 : </p>
<p> （1）一个宏，展开后为：<code>__attribute__ ((__section__ (&quot;.init.text&quot;)))</code>   实际是<code>gcc</code>的一个特殊链接标记。</p>
<p> （2）指示链接器将该函数放置在<code>.init.text</code>区段。</p>
<p> （3）在模块插入时方便内核从<code>.ko</code>文件指定位置读取入口函数的指令到特定内存位置。</p>
</blockquote>
<ul>
<li><code>module_init()</code>说明</li>
</ul>
<blockquote>
<p>（1）<code>module_init</code>是一个宏，使用格式就是：<code>module_init(模块入口函数名) </code></p>
<p>（2）动态加载模块的时候，对应的函数被调用；静态加载模块的时候，在内核启动过程中对应函数被调用。</p>
<p>（3）对于静态加载的模块其本质是定义一个全局函数指针，并将其赋值为指定函数，链接时将地址放到特殊区段（<code>.initcall</code>段），方便系统初始化统一调用。</p>
<p>（4）对于动态加载的模块，由于内核模块的默认入口函数名是<code>init_module</code>,用该宏可以给对应模块入口函数起别名。</p>
</blockquote>
<h3 id="2-2-模块卸载"><a href="#2-2-模块卸载" class="headerlink" title="2.2 模块卸载"></a><font size=3>2.2 模块卸载</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 模块出口函数 */</span></span><br><span class="line"><span class="type">void</span> __exit <span class="title function_">xxx_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 出口函数具体内容 */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module_exit(xxx_exit);  <span class="comment">/* 注册模块卸载函数 */</span></span><br></pre></td></tr></table></figure>

<p>模块卸载函数位于<code>linux/init.h</code>中，<code>module_exit</code> 函数用来向 <code>Linux</code> 内核注册一个模块卸载函数，参数 <code>xxx_exit</code> 就是需要注册的具体函数，当使用<code>rmmod</code>命令卸载驱动的时候，<code>xxx_exit</code>这个函数就会被调用，也就是说该函数在模块从内核中被移除时调用，主要作用做些<code>xxx_init</code>函数的反操作，被称为模块的出口函数。</p>
<ul>
<li><code>__exit</code>说明</li>
</ul>
<blockquote>
<p> <code>__exit</code>的作用 : </p>
<p> （1）一个宏，展开后为：<code>__attribute__ ((__section__ (&quot;.exit.text&quot;)))  </code> 实际也是<code>gcc</code>的一个特殊链接标记。</p>
<p> （2）指示链接器将该函数放置在<code>.exit.text</code>区段。</p>
<p> （3）在模块插入时方便内核从<code>.ko</code>文件指定位置读取出口函数的指令到另一个特定内存位置。</p>
</blockquote>
<ul>
<li><code>module_exit()</code>说明</li>
</ul>
<blockquote>
<p>（1）<code>module_exit</code>是一个宏，使用格式就是：<code>module_exit(模块出口函数名) </code></p>
<p>（2）动态加载模块在卸载的时候，对应的函数被调用；静态加载的模块，可以认为在系统退出时，对应函数被调用，实际上对应函数被忽略。</p>
<p>（3）对于静态加载的模块其本质是定义一个全局函数指针，并将其赋值为指定函数，链接时将地址放到特殊区段（<code>.exitcall</code>段），方便系统必要时统一调用，实际上该宏在静态加载时没有意义，因为静态编译的驱动无法卸载。</p>
<p>（4）对于动态加载的模块，由于内核模块的默认出口函数名是<code>cleanup_module</code>，用该宏可以给对应模块出口函数起别名。</p>
</blockquote>
<h2 id="3-模块信息宏"><a href="#3-模块信息宏" class="headerlink" title="3. 模块信息宏"></a><font size=3>3. 模块信息宏</font></h2><p>模块信息宏用来描述一些当前模块的信息，有一些是可选的，如<code>MODULE_DESCRIPTION</code>，有一些是必须的，如<code>MODULE_LICENSE</code>，这些宏的本质是定义<code>static</code>字符数组用于存放指定字符串内容，这些字符串内容链接时存放在<code>.modinfo</code>字段，可以用<code>modinfo</code>命令来查看这些模块信息，一般格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">modinfo  模块文件名</span><br></pre></td></tr></table></figure>

<h3 id="3-1-MODULE-LICENSE"><a href="#3-1-MODULE-LICENSE" class="headerlink" title="3.1 MODULE_LICENSE"></a><font size=3>3.1 <code>MODULE_LICENSE</code></font></h3><p><code>MODULE_LICENSE</code>是一个宏，一般使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MODULE_LICENSE(字符串常量);</span><br><span class="line"><span class="comment">/* 例如 */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>字符串常量内容为源码的许可证协议 可以是<code>GPL</code>、 <code>GPL v2</code>、<code>GPL and additional rights</code> 、<code>Dual BSD/GPL&quot;</code>、 <code>Dual MIT/GPL</code> 、<code>Dual MPL/GPL</code>等。</p>
<p>其中<code>GPL</code>最常用，其本质也是一个宏，宏体也是一个特殊链接标记，指示链接器在<code>ko</code>文件指定位置说明本模块源码遵循的许可证在模块插入到内核时，内核会检查新模块的许可证是不是也遵循<code>GPL</code>协议，如果发现不遵循<code>GPL</code>，则在插入模块时打印信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">myhello：module license &#x27;unspecified&#x27; taints kernel</span><br><span class="line">Disabling lock debugging due to kernel taint</span><br></pre></td></tr></table></figure>

<p>同时也会导致新模块没法使用一些内核其它模块提供的高级功能。</p>
<h3 id="3-2-MODULE-AUTHOR"><a href="#3-2-MODULE-AUTHOR" class="headerlink" title="3.2 MODULE_AUTHOR"></a><font size=3>3.2 <code>MODULE_AUTHOR</code></font></h3><p><code>MODULE_AUTHOR</code>是一个宏，一般使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MODULE_AUTHOR(字符串常量); <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-3-MODULE-DESCRIPTION"><a href="#3-3-MODULE-DESCRIPTION" class="headerlink" title="3.3 MODULE_DESCRIPTION"></a><font size=3>3.3 <code>MODULE_DESCRIPTION</code></font></h3><p><code>MODULE_DESCRIPTION</code>是一个宏，一般使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MODULE_DESCRIPTION(字符串常量); <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br></pre></td></tr></table></figure>

<h3 id="3-4-MODULE-ALIAS"><a href="#3-4-MODULE-ALIAS" class="headerlink" title="3.4 MODULE_ALIAS"></a><font size=3>3.4 <code>MODULE_ALIAS</code></font></h3><p><code>MODULE_ALIAS</code>是一个宏，一般使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MODULE_ALIAS(字符串常量); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br></pre></td></tr></table></figure>

<h2 id="4-模块基础框架"><a href="#4-模块基础框架" class="headerlink" title="4. 模块基础框架"></a><font size=3>4. 模块基础框架</font></h2><p>所以一个模块的编写至少需要以下几个部分：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** =====================================================</span></span><br><span class="line"><span class="comment"> * Copyright © hk. 2022-2025. All rights reserved.</span></span><br><span class="line"><span class="comment"> * File name: myhello.c</span></span><br><span class="line"><span class="comment"> * Author   : qidaink</span></span><br><span class="line"><span class="comment"> * Date     : 2022-08-23</span></span><br><span class="line"><span class="comment"> * Version  : </span></span><br><span class="line"><span class="comment"> * Description: </span></span><br><span class="line"><span class="comment"> * Others   : </span></span><br><span class="line"><span class="comment"> * Log      : </span></span><br><span class="line"><span class="comment"> * ======================================================</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span>   <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span>   <span class="comment">/* printk */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span>     <span class="comment">/* module_init module_exit */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块入口函数 */</span></span><br><span class="line"><span class="type">int</span> __init <span class="title function_">xxx_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 入口函数具体内容 */</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块出口函数 */</span></span><br><span class="line"><span class="type">void</span> __exit <span class="title function_">xxx_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 出口函数具体内容 */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 将上面两个函数指定为驱动的入口和出口函数 */</span></span><br><span class="line">module_init(xxx_init);<span class="comment">/* 注册模块加载（入口）函数 */</span></span><br><span class="line">module_exit(xxx_exit);<span class="comment">/* 注册模块卸载（出口）函数 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块信息(通过 modinfo xxx 查看) */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL&quot;</span>);               <span class="comment">/* 源码的许可证协议 */</span></span><br><span class="line">MODULE_AUTHOR(<span class="string">&quot;qidaink&quot;</span>);            <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br><span class="line">MODULE_DESCRIPTION(<span class="string">&quot;Description&quot;</span>);   <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br><span class="line">MODULE_ALIAS(<span class="string">&quot;module&#x27;s other name&quot;</span>); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br></pre></td></tr></table></figure>

<h1 id="四、Makefile"><a href="#四、Makefile" class="headerlink" title="四、Makefile"></a><font size=3>四、<code>Makefile</code></font></h1><h2 id="1-代码解析"><a href="#1-代码解析" class="headerlink" title="1. 代码解析"></a><font size=3>1. 代码解析</font></h2><h3 id="1-1-几个变量"><a href="#1-1-几个变量" class="headerlink" title="1.1 几个变量"></a><font size=3>1.1 几个变量</font></h3><p>为了方便使用，这里定义了几个变量：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 模块名</span></span><br><span class="line">MODULE_NAME      := myhello</span><br><span class="line"><span class="comment"># NFS 共享目录</span></span><br><span class="line">ROOTFS           ?= /home/hk/4nfs/rootfs</span><br><span class="line">MODULE_DIR       ?= <span class="variable">$(ROOTFS)</span>/01myDrivers</span><br></pre></td></tr></table></figure>

<ul>
<li><code>MODULE_NAME</code>：就是这个模块的名称。</li>
<li><code>ROOTFS</code>：就是<code>NFS</code>共享目录的路径，这里其实是代表了网络挂载的开发板的根文件系统的路径。</li>
<li><code>MODULE_DIR</code>：根文件系统下的驱动存放的目录，我们要在开发板加载驱动模块，需要先将模块拷贝到开发板，这个就是存放的目录路径。</li>
</ul>
<h3 id="1-2-选择内核源码目录"><a href="#1-2-选择内核源码目录" class="headerlink" title="1.2 选择内核源码目录"></a><font size=3>1.2 选择内核源码目录</font></h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 选择可执行文件运行的平台</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(ARCH)</span>, arm)</span><br><span class="line">KERNELDIR        ?= /home/hk/5linux/linux-3.14</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">KERNELDIR        ?= /lib/modules/<span class="variable">$(<span class="built_in">shell</span> uname -r)</span>/build</span><br><span class="line"><span class="keyword">endif</span></span><br></pre></td></tr></table></figure>

<ul>
<li><code>ARCH</code>：我们使用<code>make</code>参数的时候指定的，我们使用<code>make ARCH=arm</code>的时候，这里的<code>ARCH</code>就是<code>arm</code>。</li>
<li><code>KERNELDIR</code>：开发板所使用的<code>Linux</code>内核源码目录，使用绝对路径 。</li>
</ul>
<p>这一段，根据我们<code>make</code>的时候参数的不同，可以编译出用于<code>x86</code>平台和<code>arm</code>平台的<code>.ko</code>文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">make          # 编译可在Ubuntu中运行的模块</span><br><span class="line">make ARCH=arm # 编译可在ARM上运行的模块</span><br></pre></td></tr></table></figure>

<h3 id="1-3-编译命令"><a href="#1-3-编译命令" class="headerlink" title="1.3 编译命令"></a><font size=3>1.3 编译命令</font></h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">PWD := <span class="variable">$(<span class="built_in">shell</span> pwd)</span></span><br><span class="line"></span><br><span class="line"><span class="section">modules:</span></span><br><span class="line">	<span class="variable">$(MAKE)</span> -C <span class="variable">$(KERNELDIR)</span> M=<span class="variable">$(PWD)</span> modules</span><br></pre></td></tr></table></figure>

<ul>
<li><code>-C</code>：表示将当前的工作目录切换到指定目录中 ，也就是<code>KERNERLDIR</code>目录  。</li>
<li><code>M</code>：表示模块源码目录，<code>make modules</code>命令中加入<code>M=dir</code>以后程序会自动到指定的<code>dir</code>目录中读取模块的源码并将其编译为<code>.ko</code>文件。  </li>
<li><code>modules</code>： 表示编译模块 。</li>
</ul>
<h3 id="1-4-obj-m"><a href="#1-4-obj-m" class="headerlink" title="1.4 obj-m "></a><font size=3>1.4 <code>obj-m </code></font></h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj-m            += <span class="variable">$(MODULE_NAME)</span>.o</span><br></pre></td></tr></table></figure>

<ul>
<li><code>obj-m</code>：它后边是目标文件名，表示将<code>$(MODULE_NAME).c</code>编译成<code>$(MODULE_NAME).ko</code>模块。</li>
</ul>
<h2 id="2-多源文件编译"><a href="#2-多源文件编译" class="headerlink" title="2. 多源文件编译"></a><font size=3>2. 多源文件编译</font></h2><p>有时候我们要是一个模块依赖于多个<code>.c</code>源文件怎么编译呢？我们修改<code>Makefile</code>的<code>obj-m</code>部分即可：</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">obj-m            += <span class="variable">$(MODULE_NAME)</span>.o</span><br><span class="line"></span><br><span class="line"><span class="variable">$(MODULE_NAME)</span>-objs = <span class="variable">$(DEPEND1)</span>.o <span class="variable">$(DEPEND2)</span>.o</span><br></pre></td></tr></table></figure>

<p>例如，</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <div class="tabs" id="tabname1"><ul class="nav-tabs"><li class="tab active"><a href="#tabname1-1">test.c</a></li><li class="tab"><a href="#tabname1-2">func.c</a></li><li class="tab"><a href="#tabname1-3">Makefile</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname1-1"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span>   <span class="comment">/* MODULE_LICENSE */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span>     <span class="comment">/* module_init module_exit */</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">test_func</span><span class="params">(<span class="type">void</span>)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块入口函数 */</span></span><br><span class="line"><span class="type">int</span> __init <span class="title function_">test_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	test_func();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块出口函数 */</span></span><br><span class="line"><span class="type">void</span> __exit <span class="title function_">test_exit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	printk(<span class="string">&quot;test will exit\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 将上面两个函数指定为驱动的入口和出口函数 */</span></span><br><span class="line">module_init(test_init);</span><br><span class="line">module_exit(test_exit);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 模块信息(通过 modinfo test 查看) */</span></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL&quot;</span>);               <span class="comment">/* 源码的许可证协议 */</span></span><br><span class="line">MODULE_AUTHOR(<span class="string">&quot;qidaink&quot;</span>);            <span class="comment">/* 字符串常量内容为模块作者说明 */</span></span><br><span class="line">MODULE_DESCRIPTION(<span class="string">&quot;Description&quot;</span>);   <span class="comment">/* 字符串常量内容为模块功能说明 */</span></span><br><span class="line">MODULE_ALIAS(<span class="string">&quot;module&#x27;s other name&quot;</span>); <span class="comment">/* 字符串常量内容为模块别名 */</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname1-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/kernel.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">test_func</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* code */</span> </span><br><span class="line">    printk(<span class="string">&quot;this is test_func\n&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname1-3"><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">##============================================================================#</span></span><br><span class="line"><span class="comment"># Copyright © hk. 2022-2025. All rights reserved.</span></span><br><span class="line"><span class="comment"># File name  : Makefile</span></span><br><span class="line"><span class="comment"># Author     : qidaink</span></span><br><span class="line"><span class="comment"># Date       : 2022-08-28</span></span><br><span class="line"><span class="comment"># Version    : </span></span><br><span class="line"><span class="comment"># Description: 驱动模块编译Makefile</span></span><br><span class="line"><span class="comment"># Others     : </span></span><br><span class="line"><span class="comment"># Log        : </span></span><br><span class="line"><span class="comment">##============================================================================#</span></span><br><span class="line"><span class="comment">##</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 模块名和模块测试APP名称</span></span><br><span class="line">MODULE_NAME           := mytest</span><br><span class="line"><span class="comment"># NFS 共享目录</span></span><br><span class="line">ROOTFS                ?= /home/hk/4nfs/rootfs</span><br><span class="line">MODULE_DIR            ?= <span class="variable">$(ROOTFS)</span>/01myDrivers</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(KERNELRELEASE)</span>,)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选择可执行文件运行的平台</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(ARCH)</span>, arm)</span><br><span class="line">KERNELDIR             ?= /home/hk/5linux/linux-3.14</span><br><span class="line"><span class="keyword">else</span> </span><br><span class="line">KERNELDIR             ?= /lib/modules/<span class="variable">$(<span class="built_in">shell</span> uname -r)</span>/build</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line">PWD                   := <span class="variable">$(<span class="built_in">shell</span> pwd)</span></span><br><span class="line"><span class="comment"># 编译模块和测试程序</span></span><br><span class="line"><span class="section">modules:</span></span><br><span class="line">	<span class="variable">$(MAKE)</span> -C <span class="variable">$(KERNELDIR)</span> M=<span class="variable">$(PWD)</span> modules</span><br><span class="line"></span><br><span class="line"><span class="section">modules_install:</span></span><br><span class="line">	<span class="variable">$(MAKE)</span> -C <span class="variable">$(KERNELDIR)</span> M=<span class="variable">$(PWD)</span> modules INSTALL_MOD_PATH=<span class="variable">$(ROOTFS)</span> modules_install</span><br><span class="line"></span><br><span class="line"><span class="comment"># 拷贝相关文件到nfs共享目录</span></span><br><span class="line"><span class="section">copy:</span></span><br><span class="line">	sudo cp <span class="variable">$(MODULE_NAME)</span>.ko <span class="variable">$(MODULE_DIR)</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf  *.o  *.ko  .*.cmd  *.mod.*  modules.order  Module.symvers   .tmp_versions .cache.mk</span><br><span class="line"></span><br><span class="line"><span class="section">help:</span></span><br><span class="line">	@echo <span class="string">&quot;\033[1;32m================================ Help ================================\033[0m&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;Ubuntu may need to add sudo:&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;insmod &lt;module_name&gt;.ko   # Load module&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;rmmod &lt;module_name&gt;       # Uninstall the module&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;dmesg -C                  # Clear the kernel print information&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;lsmod                     # Check the kernel modules that have been inserted&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;dmesg                     # View information printed by the kernel&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;file &lt;module_name&gt;.ko     # View \&quot;.Ko\&quot; file information&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;make ARCH=arm             # arm platform&quot;</span></span><br><span class="line">	@echo <span class="string">&quot;\033[1;32m======================================================================\033[0m&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="section">print:</span></span><br><span class="line">	@echo <span class="string">&quot;KERNELDIR = <span class="variable">$(KERNELDIR)</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">CONFIG_MODULE_SIG=n</span><br><span class="line">obj-m                 += <span class="variable">$(MODULE_NAME)</span>.o</span><br><span class="line"><span class="variable">$(MODULE_NAME)</span>-objs   := test.o func.o</span><br><span class="line"><span class="keyword">endif</span></span><br></pre></td></tr></table></figure></div></div></div>
              </div>
            </details>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/b24f362c.html">LV10-01-内核模块-01-编译与加载</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月08日 - 18:58</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/b24f362c.html" title="LV10-01-内核模块-01-编译与加载">https://sumumm.github.io/post/b24f362c.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV10-驱动开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/62647cd9.html" rel="prev" title="LV10-01-内核模块-02-模块参数与依赖">
                  <i class="fa fa-angle-left"></i> LV10-01-内核模块-02-模块参数与依赖
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/5db09a14.html" rel="next" title="LV09-03-linux-03-USB驱动移植">
                  LV09-03-linux-03-USB驱动移植 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
